package io.renren.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.dao.*;
import io.renren.common.dto.SetmealDTO;
import io.renren.common.entity.*;
import io.renren.common.redis.RedisUtils;
import io.renren.common.service.SetmealService;
import io.renren.common.service.impl.CrudServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 套餐
 *
 * @author yidian ${email}
 * @since 1.0.0 2022-11-06
 */
@Service
public class SetmealServiceImpl extends CrudServiceImpl<SetmealDao, SetmealEntity, SetmealDTO> implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealDishDao setmealDishDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public QueryWrapper<SetmealEntity> getWrapper(Map<String, Object> params){
        String name = (String)params.get("name");
        String code = (String)params.get("code");
        QueryWrapper<SetmealEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), "name", name);
        wrapper.eq(StringUtils.isNotBlank(code), "code", code);

        String sPrice =(String)params.get("minPrice");
        String bPrice =(String)params.get("maxPrice");
        wrapper.ge(StringUtils.isNotBlank(sPrice), "price", sPrice);
        wrapper.le(StringUtils.isNotBlank(bPrice), "price", bPrice);

        return wrapper;
    }


    @Override
    public void getByWithSetmealDish(SetmealDTO setmealDTO, Long id) {
        LambdaQueryWrapper<SetmealDishEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDishEntity::getSetmealId,id);
        List<SetmealDishEntity> setmealDishEntityList = setmealDishDao.selectList(queryWrapper);
        setmealDTO.setSetmealDishes(setmealDishEntityList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SetmealDTO> findSetmealByCategoryId(SetmealDTO setmealDTO) {
        List<SetmealDTO> setmealDTOList = new ArrayList<>();

        LambdaQueryWrapper<SetmealEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmealDTO.getCategoryId() != null, SetmealEntity::getCategoryId, setmealDTO.getCategoryId());
        queryWrapper.eq(setmealDTO.getStatus() != null, SetmealEntity::getStatus, setmealDTO.getStatus());
        queryWrapper.orderByDesc(SetmealEntity::getUpdateDate);
       List<SetmealEntity> list = setmealDao.selectList(queryWrapper);
        for (SetmealEntity setmealEntity : list) {
            SetmealDTO dto = new SetmealDTO();
            BeanUtils.copyProperties(setmealEntity,dto);
            Long categoryId = setmealEntity.getCategoryId();
            CategoryEntity category = categoryDao.selectById(categoryId);
            if (category != null){
                String name = category.getName();
                dto.setCategoryName(name);
            }
            Long id = dto.getId();
            getByWithSetmealDish(dto,id);
            setmealDTOList.add(dto);
        }


            return setmealDTOList;



    }


}